#![feature(custom_attribute)]

#[macro_use]
extern crate serde_derive;

extern crate bytes;
extern crate failure;
extern crate futures;
extern crate actix;
extern crate actix_web;
extern crate listenfd;
extern crate http;
extern crate serde;
extern crate serde_json;
extern crate env_logger;
extern crate openssl;

mod app;
mod handler;
mod middleware;

use listenfd::ListenFd;
use actix_web::{server};
use app::create_application;

fn main() {
    ::std::env::set_var("RUST_LOG", "actix_web=debug");
    env_logger::init();

    let mut listenfd = ListenFd::from_env();
    let mut server = server::new(create_application);

    server = if let Some(l) = listenfd.take_tcp_listener(0).unwrap() {
        server.listen(l)
    } else {
//        // load ssl keys
//        use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
//        let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
//        builder
//            .set_private_key_file("/Users/didi/Documents/work/actix/localhost.key", SslFiletype::PEM)
//            .unwrap();
//        builder.set_certificate_chain_file("/Users/didi/Documents/work/actix/localhost.crt").unwrap();
//
//        server.bind_ssl("127.0.0.1:8000", builder).unwrap()
        server.bind("127.0.0.1:8000").unwrap()
    };

    server.run();
}
